//
//  MerchantModel.m
//  RainbowFM
//
//  Created by Kratos on 14/12/23.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MerchantModel.h"

#import "StoreAPI.h"

@implementation MerchantModel

+(instancetype)sharedModel
{
    static MerchantModel *merchantModel = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        merchantModel = [[MerchantModel alloc] init];
    });
    
    return merchantModel;
}

/**
 * 商家分类列表
 * @return <#return value description#>
 * @author Kratos
 */
-(void)fetchMerchantTypeList:(void(^)(NSArray *typeList, NSString *msg))res
{
    StoreAPI *api = [StoreAPI new];
    [api fetchMerchantTypeListWithuserId:nil userCode:nil result:^(NSArray *typeList, NSString *msg) {
        NSMutableArray *list = [NSMutableArray array];
        for (NSDictionary *dic in typeList) {
            MerchantTypeItem *item = [[MerchantTypeItem alloc]initWithDataDic:dic];
            [list addObject:item];
        }
        
        res(list,msg);
    }];
}

/**
 * 查询商家列表
 * @param colId 栏目ID N
 * @param key 关键字 N
 * @param typeId 分类ID N
 * @param currentPage 当前页 Y
 * @param pageSize 每页条数 Y
 * @return <#return value description#>
 * @author Kratos
 */
-(void)fetchMerchantsListWithColId:(NSString *)colId
                               key:(NSString *)key
                            typeId:(NSString *)typeId
                       currentPage:(int)currentPage
                          pageSize:(int)pageSize
                            result:(void(^)(NSArray *list, NSString *msg))res
{
    StoreAPI *api = [StoreAPI new];
    [api fetchMerchantsListWithuserId:nil userCode:nil colId:colId key:key typeId:typeId currentPage:_F(@"%d",currentPage) pageSize:_F(@"%d",pageSize) result:^(NSArray *list, NSString *msg) {
        NSMutableArray *itemslist = [NSMutableArray array];
        for (NSDictionary *dic in list) {
            MerchantItem *item = [[MerchantItem alloc]initWithDataDic:dic];
            [itemslist addObject:item];
        }
        
        res(itemslist,msg);
    }];
}

/**
 * 商家详情
 * @param merchantId <#merchantId description#>
 * @return <#return value description#>
 * @author Kratos
 */
-(void)searchMerchantDetailWithMerchantId:(NSString *)merchantId
                                   result:(void(^)(MerchantItem *merchant, NSString *msg))res
{
    StoreAPI *api = [StoreAPI new];
    [api searchMerchantDetailWithuserId:nil userCode:nil merchantId:merchantId result:^(NSDictionary *merchant, NSString *msg) {
        
        MerchantItem *item  = nil;
        if (merchant) {
            item = [[MerchantItem alloc]initWithDataDic:merchant];
        }
        
        res(item,msg);
    }];
}
@end



@implementation MerchantTypeItem

-(NSDictionary *)attributeMapDictionary
{
    return @{@"TYPE_ID":@"TYPE_ID",
             @"TYPE_NAME":@"TYPE_NAME"};
}

@end

@implementation MerchantItem

-(NSDictionary *)attributeMapDictionary
{
    return @{@"MERCHANT_ID":@"MERCHANT_ID",
             @"MERCHANT_NAME":@"MERCHANT_NAME",
             @"MERCHANT_DESC":@"MERCHANT_DESC",
             @"MERCHANT_IMG_URL":@"MERCHANT_IMG_URL",
             @"PHONE":@"PHONE",
             @"mediaList":@"mediaList",
             @"productList":@"productList"};
}

-(NSArray *)mediaItemList{
    NSMutableArray *list = [NSMutableArray array];
    for (NSDictionary *dic in _mediaList) {
        MediaItem *item = [[MediaItem alloc]initWithDataDic:dic];
        [list addObject:item];
    }
    
    return list;

}

-(NSArray *)productItemList
{
    NSMutableArray *list = [NSMutableArray array];
    for (NSDictionary *dic in _productList) {
        GoodsInfoItem *item = [[GoodsInfoItem alloc]initWithDataDic:dic];
        [list addObject:item];
    }
    
    return list;
}

@end